using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using TrenitaliaWP7.Resources;
using Services.Menu;

namespace TrenitaliaWP7
{
    public enum GameTitle
    { 
        SpaceBattle,
        Enigma,
        EbookReader,
        Ideopuzzle,
        //Crossroads,
        RisikoDice,
        Flashlight,
        Dice3D
    }

    public class AdsComponent : OrientableScreen
    {
        private List<GameTitle> game_to_ad;
        private Texture2D bg;
        private List<Texture2D> games_tile;
        private List<Vector2> tiles_position;
        private Vector2 bg_pos, fr_s, sn_s;
        private SpriteBatch spriteBatch;
        private SpriteFont font;

        private float time_before_exit, elapsed_time, bg_rot;
        List<GameTitle> chosen;
        int n;
        public AdsComponent(Game game, List<GameTitle> game_to_ad, float time_before_exit)
            : base(game)
        {
            this.time_before_exit = time_before_exit;
            this.game_to_ad = game_to_ad;

            Strings.Culture = System.Globalization.CultureInfo.CurrentCulture;
        }

        public override void Initialize()
        {
            elapsed_time = time_before_exit;
            bg_rot = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            //var isLandscape = Game.GraphicsDevice.Viewport.Width > Game.GraphicsDevice.Viewport.Height ? true : false;
            var random = new Random(DateTime.Now.Millisecond);

            games_tile = new List<Texture2D>();
            tiles_position = new List<Vector2>();
            
            n = Math.Min(5, game_to_ad.Count);
            chosen = new List<GameTitle>(n);
            chosen.Add(GameTitle.Enigma);
            for (int i = 0; i < n; i++)
            {
                //GameTitle val;
                //bool ok = true;
                //do
                //{
                //    val = (GameTitle)random.Next(game_to_ad.Count);
                //    ok = chosen.Contains(val);
                //} while (ok);
                int a = random.Next(game_to_ad.Count);
                chosen.Add(game_to_ad[a]);
                game_to_ad.RemoveAt(a);
            }
            //for (int i = 1; i <= n; i++)
            //{
            //    var ind = random.Next(game_to_ad.Count);

            //    games_tile.Add(GetGameTile(game_to_ad[ind]));
            //    tiles_position.Add(GetTilePosition(i));

            //    game_to_ad.RemoveAt(ind);
            //}

            bg = Game.Content.Load<Texture2D>("Shared/ads_bg");
            font = Game.Content.Load<SpriteFont>("Fonts/Regular30");

            //if (isLandscape)
            //{
            //    bg_pos = Vector2.Zero;

            //    fr_s = new Vector2(190 - font.MeasureString(Strings.First_S).X / 2.0f, 60);
            //    sn_s = new Vector2(190 - font.MeasureString(Strings.Second_S).X / 2.0f, 110);
            //}
            //else
            //{
            //    bg_rot = MathHelper.ToRadians(90);
            //    bg_pos = new Vector2(bg.Height, 0);
                
            //    fr_s = new Vector2(130 - font.MeasureString(Strings.First_S).X / 2.0f, 90);
            //    sn_s = new Vector2(130 - font.MeasureString(Strings.Second_S).X / 2.0f, 135);
            //}

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            elapsed_time -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (elapsed_time <= 0)
                Game.Exit();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(bg, bg_pos, null, Color.White, bg_rot, Vector2.Zero, 1, SpriteEffects.None, 0);
            //spriteBatch.Draw(bg, bg_pos, Color.White);
            for (int i = 0; i < games_tile.Count; i++)
                spriteBatch.Draw(games_tile[i], tiles_position[i], Color.White);

            spriteBatch.DrawString(font, Strings.First_S, fr_s, Color.White);
            spriteBatch.DrawString(font, Strings.Second_S, sn_s, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private Texture2D GetGameTile(GameTitle g)
        {
            return Game.Content.Load<Texture2D>("GameTiles/" + g.ToString());
        }

        //private Vector2 GetTilePosition(int i)
        //{
        //    var isLandscape = Game.GraphicsDevice.Viewport.Width > Game.GraphicsDevice.Viewport.Height ? true : false;

        //    if (isLandscape)
        //    {
        //        switch (i)
        //        { 
        //            case 1:
        //                return new Vector2(330, 16);
        //            case 2:
        //                return new Vector2(536, 16);
        //            case 3:
        //                return new Vector2(102, 243);
        //            case 4:
        //                return new Vector2(330, 243);
        //            default:
        //                return new Vector2(536, 243);
        //        }
        //    }
        //    else
        //    {
        //        switch (i)
        //        {
        //            case 1:
        //                return new Vector2(256,31);
        //            case 2:
        //                return new Vector2(48, 276);
        //            case 3:
        //                return new Vector2(256, 276);
        //            case 4:
        //                return new Vector2(48, 542);
        //            default:
        //                return new Vector2(256, 542);
        //        }
        //    }
        //}

        public override void SetPortraitOrientation()
        {
            bg_rot = MathHelper.ToRadians(90);
            bg_pos = new Vector2(bg.Height, 0);
            
            fr_s = new Vector2(130 - font.MeasureString(Strings.First_S).X / 2.0f, 90);
            sn_s = new Vector2(130 - font.MeasureString(Strings.Second_S).X / 2.0f, 135);
            tiles_position.Clear();
            games_tile.Clear();
            for (int i = 0; i < n; i++)
            {
                var ind = chosen[i];
                switch (i)
                {
                    case 0:
                        tiles_position.Add( new Vector2(256, 31));
                        break;
                    case 1:
                        tiles_position.Add( new Vector2(48, 276));
                        break;
                    case 2:
                        tiles_position.Add( new Vector2(256, 276));
                        break;
                    case 3:
                        tiles_position.Add( new Vector2(48, 542));
                        break;
                    default:
                        tiles_position.Add( new Vector2(256, 542));
                        break;
                }
                games_tile.Add(GetGameTile((GameTitle)chosen[i]));
                //tiles_position.Add(GetTilePosition(i));

                //game_to_ad.RemoveAt(ind);
            }
        }

        public override void SetLandScapeOrientation()
        {
            bg_pos = Vector2.Zero;
            bg_rot = 0;
            fr_s = new Vector2(190 - font.MeasureString(Strings.First_S).X / 2.0f, 60);
            sn_s = new Vector2(190 - font.MeasureString(Strings.Second_S).X / 2.0f, 110);
            tiles_position.Clear();
            games_tile.Clear();

            for (int i = 0; i < n; i++)
            {
                var ind = chosen[i];
                switch (i)
                {
                    case 0:
                        tiles_position.Add( new Vector2(330, 16));
                        break;
                    case 1:
                        tiles_position.Add( new Vector2(536, 16));
                        break;
                    case 2:
                        tiles_position.Add( new Vector2(102, 243));
                        break;
                    case 3:
                        tiles_position.Add( new Vector2(330, 243));
                        break;
                    default:
                        tiles_position.Add( new Vector2(536, 243));
                        break;
                }

                games_tile.Add(GetGameTile((GameTitle)chosen[i]));
                //tiles_position.Add(GetTilePosition(i));

                
            }
        }
    }
}
